package csa_test

import (
	"git.sr.ht/~mil/mobroute/src/csa"
	"git.sr.ht/~mil/mobroute/src/logger"
	"reflect"
	"testing"
)

func Test_ExecuteCSAMainLoop(t *testing.T) {
	logger.LogWarn = true

	for _, testCase := range []CSATestCase{
		csaTestCaseBasic,
		csaTestCaseImplicitOk,
		csaTestCaseImplicitNop,
		csaTestCaseExplicitOk,
		csaTestCaseExplicitNop,
		csaTestCaseMultitransExplicit,
		csaTestCaseMultitransImplicit,
	} {
		resultArrivalDetails := csa.ExecuteCSAMainLoop(
			testCase.Args.Connections, testCase.Args.Transfers,
			testCase.Args.WalkcostsOrigin, testCase.Args.FromTime,
			testCase.Args.ImplicitTransferSeconds, testCase.Args.MaxTransferSeconds,
			testCase.Args.MaxNTransfers,
		)

		// E.g. lets us test against / mock ordering of conndis not raw ptrs
		resultMapped := CSAArrivalDetailsToCSATestArrivalDetails(resultArrivalDetails)

		if !reflect.DeepEqual(testCase.Expect, resultMapped) {
			t.Fatalf("CSAExecuteMainLoop (%v) incorrect arrival details want: %v has: %v", testCase.Test, testCase.Expect, resultMapped)
		}
	}
}

// Maps the CSA arrival
func CSAArrivalDetailsToCSATestArrivalDetails(mapArrivalDetails map[string]csa.ArrivalDetails) map[string]CSATestArrivalDetails {
	m := map[string]CSATestArrivalDetails{}
	for stop, arrivalDetails := range mapArrivalDetails {
		a := CSATestArrivalDetails{Time: arrivalDetails.Time, NTrips: arrivalDetails.NTrips}
		if arrivalDetails.Connection != nil {
			a.ConnID = arrivalDetails.Connection.ConnID
		} else {
			a.ConnID = ""
		}
		m[stop] = a
	}
	return m
}
